package com.itboye.textileexpres.myset;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPUtils;

public class ActivityPersonEmail extends BaseActivity implements
		OnClickListener, Observer, TextWatcher {
	ImageView back;
	TextView title;
	ImageView img_other;
	MainPresenter mainPresenter;
	EditText edit;
	Button btnOk;
	String nick;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_person_email;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		title.setText("邮箱");
		mainPresenter = new MainPresenter(this);
		edit.addTextChangedListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.btnOk:
			onBtnSend();
			break;
		default:
			break;
		}
	}

	public void onBtnSend() {
		nick = edit.getText() + "";
		if (!IsMobileNumPhone.isEmail(nick)) {
			toast("邮箱格式不正确！");
		} else {
			UserBean bean = new UserBean();
			bean.setUid(IsMobileNumPhone.isUid());
			bean.setEmail(nick);
			mainPresenter.updateUserInfoByUid(bean);

		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					MainPresenter.UpdateUserInfoSuccessEvent)) {
				Intent intent = new Intent("updatechange");
				sendBroadcast(intent);
				SPUtils.put(this, null, SpPublicParams.Email, nick);
				finish();
			} else if (entity.getEventType().equals(
					MainPresenter.UpdateUserInfoFailureEvent)) {
				toast("" + entity.getData());
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (edit.getText().toString().length() > 0) {
			btnOk.setEnabled(true);
			Log.e("sbsbsb", edit.getText().toString());
		} else {
			Log.e("sbsbsb", edit.getText().toString());
			btnOk.setEnabled(false);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
